package msu.erdc.hci;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import msu.erdc.hci.DateDialogFragment.DateDialogFragmentListener;
import msu.erdc.hci.customObjects.InsuranceObject;
import msu.erdc.hci.customObjects.VehicleACObject;
import msu.erdc.hci.customObjects.VehicleObject;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class AccidentVehicle extends Fragment {
	
	int accidentIndex;
	int vehicleIndex;
	VehicleACObject vehicle;
	TextView breadCrumb;
	EditText tagNumber;
	EditText vinNumber;
	EditText color;
	Spinner makeSpinner;
	Spinner modelSpinner;
	Spinner yearSpinner;
	List<String> makeList;
	List<String> modelList;
	List<String> yearList;
	Button occupantsButton;
	Button damageReportButton;
	List<String> dodgeModels = Arrays.asList(new String[]{"Ram 1500", "Charger", "Challenger"});
	List<String> fordModels = Arrays.asList(new String[]{"F-150", "Ranger", "Escape"});
	List<String> hondaModels = Arrays.asList(new String[]{"Accord", "CRV", "Pilot"});
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.accident_vehicle, container, false);
		final FragmentManager manager = getFragmentManager();
		
		breadCrumb = (TextView) view.findViewById(R.id.breadTextView);
		breadCrumb.setText(((Global)getActivity().getApplication()).accidents.get(accidentIndex).toString() + "/Vehicle:");
		
		tagNumber = (EditText) view.findViewById(R.id.tagEditText);
		tagNumber.setText(vehicle.tagNumber);
		
		vinNumber = (EditText) view.findViewById(R.id.vinEditText);
		vinNumber.setText(vehicle.vinNumber);
		
		color = (EditText) view.findViewById(R.id.colorEditText);
		color.setText(vehicle.color);
		
		makeSpinner = (Spinner) view.findViewById(R.id.makeSpinner);
		int makeIndex = makeList.indexOf(vehicle.make);
		
		yearSpinner = (Spinner) view.findViewById(R.id.yearSpinner);
		int yearIndex = yearList.indexOf(String.valueOf(vehicle.year));
		if(yearIndex > -1)
			yearSpinner.setSelection(yearIndex);
		
		if(makeIndex > -1)
			makeSpinner.setSelection(makeIndex);
		makeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				updateModelSpinner();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				//do nothing
			}
		});
		modelSpinner = (Spinner) view.findViewById(R.id.modelSpinner);
		
		occupantsButton = (Button) view.findViewById(R.id.occupantsButton);
		occupantsButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				
				VehicleOccupantList occupants = new VehicleOccupantList();
				Bundle arguments = new Bundle();
				
				arguments.putInt("accidentIndex", accidentIndex);
				arguments.putInt("vehicleIndex", vehicleIndex);
				occupants.setArguments(arguments);
				
				ft.replace(android.R.id.content, occupants, "VehicleOccupantsList");
				ft.addToBackStack(null);
				ft.commit();
				
			}});
		
		damageReportButton = (Button) view.findViewById(R.id.damageReportButton);
		damageReportButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				
				DamageReport report = new DamageReport();
				Bundle arguments = new Bundle();
				
				arguments.putInt("accidentIndex", accidentIndex);
				arguments.putInt("vehicleIndex", vehicleIndex);
				report.setArguments(arguments);
				
				ft.replace(android.R.id.content, report, "DamageReport");
				ft.addToBackStack(null);
				ft.commit();
				
			}});

		return view;
	}
	
	private void updateModelSpinner()
	{
		int modelIndex = 0;
		SpinnerAdapter modelAdapter = null;
		if(makeSpinner.getSelectedItem().equals("Dodge"))
		{
			modelAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dodgeModels);
			modelIndex = dodgeModels.indexOf(vehicle.model);
		}
		else if(makeSpinner.getSelectedItem().equals("Ford"))
		{
			modelAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, fordModels);
			modelIndex = fordModels.indexOf(vehicle.model);
		}
		else if(makeSpinner.getSelectedItem().equals("Honda"))
		{
			modelAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, hondaModels);
			modelIndex = hondaModels.indexOf(vehicle.model);
		}
			
		modelSpinner.setAdapter(modelAdapter);
		if(modelIndex > -1)
			modelSpinner.setSelection(modelIndex);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		 Bundle arguments = getArguments();
		 accidentIndex = arguments.getInt("accidentIndex");
		 vehicleIndex =  arguments.getInt("vehicleIndex");
		 
	        if(!arguments.containsKey("vehicleIndex"))
	        	vehicle = new VehicleACObject();
	        else
	        	vehicle = ((Global)getActivity().getApplication()).accidents.get(arguments.getInt("accidentIndex")).reportItemList.vehicleList.get(arguments.getInt("vehicleIndex"));
			
			yearList = Arrays.asList(getResources().getStringArray(R.array.years));
			makeList = Arrays.asList(getResources().getStringArray(R.array.makes));
	}
	
	@Override
	public void onPause(){
		super.onPause();
		// Save data
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}